package logic

import (
	"api/pkg"
	"context"
	"net/http"
	"time"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UploadLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUploadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadLogic {
	return &UploadLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UploadLogic) Upload(req *http.Request) (resp *types.Response, err error) {
	file, m, err := req.FormFile("img")

	if err != nil {
		return &types.Response{
			Message: err.Error(),
			Code:    500,
			Data:    nil,
		}, nil
	}

	if m.Size > 5*1024*1024 {
		return &types.Response{
			Message: "文件过大",
			Code:    500,
			Data:    nil,
		}, nil
	}

	filename := time.Now().Format(m.Filename)
	yun, err := pkg.QiNiuYun(file, filename)

	return &types.Response{
		Message: "上传成功",
		Code:    200,
		Data:    yun,
	}, nil

}
